/*
 * Contains methods related to cookies handling and making request.
 */
package com.unsanctioned.radioagregator.http.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import com.unsanctioned.radioagregator.utils.AppConfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class CookieManager {
	public static final String DOGTAG = CookieManager.class.getSimpleName();
	private static CookieManager instance = null;
	private Context context = null;
	private AppConfig appConfig;
	/**
	 * Default constructor.
	 * @param context - a context from nearby activity.
	 */
	public CookieManager(Context context) {
		this.context = context;
		this.instance = this;
		this.appConfig = AppConfig.getInstance(context);
	}
	public static CookieManager getInstance(Context context) {
		if (instance == null) {
			return new CookieManager(context);
		} else
			return instance;
	}
	/**
	 * Creates custom RestTemplate instance based on provided role.
	 * @param role - for now it's either <b>Client</b> or <b>User</b>.
	 * @return instance of RestTemplate, otherwise <b>null</b>.
	 */
	public RestTemplate getCustomRestTemplate(String domain) {
		RestTemplate rest = new RestTemplate(true);
		if (appConfig.getBoolean("radio-prepared")) {
			if (domain == "User") {
				String cookie = appConfig.getString("user-cookie");
				return new RestTemplate(true, new ClientRequestFactory("Cookie", cookie));
			} else if (domain == "Client") {
				String cookie = appConfig.getString("client-cookie");
				return new RestTemplate(true, new ClientRequestFactory("Cookie", cookie));
			} else return new RestTemplate(true);
		} else return new RestTemplate(true);
	}
	/**
	 * Parse single line String of cookie into several section of cookies
	 * @param cookie - a raw cookie from previous HTTP Request.
	 * @return an array containing values of provided cookie.
	 */
	public String[] parseCookie(String cookie) {
		Log.i(DOGTAG, "parsing cookie: " + cookie);
		
		// Remove any spaces from cookie
		cookie = StringUtils.deleteAny(cookie, " ");
		String[] cookies = null, result = null;
		
		// Divide cookie based on commas.
		cookies = StringUtils.delimitedListToStringArray(cookie, ",");
		
		// Divide cookies based on semicolons.
		int length = cookies.length;
		for(int i=0; i<length; ++i) {
			cookies[i] = StringUtils.split(cookies[i], ";")[0];
		}
		
		// Obtain cookie: name and value.
		List<String> listOfCookie = new ArrayList<String>();
		for(int j=0; j<cookies.length; ++j) {
			String[] temp = StringUtils.split(cookies[j], "=");
			if(temp != null) {
				listOfCookie.add(cookies[j]);
				// if(temp[0].contains("PHPSESSID") || temp[0].contains("JSESSIONID") || temp[0].contains("wp_cr") || temp[0].contains("wordpress")) {
					// listOfCookie.add(cookies[j]);
				// } 
			}
		}
		
		// Debugging and finalizing
		result = new String[listOfCookie.size()];
		for(int k=0; k<listOfCookie.size(); ++k) {
			result[k] = listOfCookie.get(k);
		}
		
		return result;
	}
}